﻿using System;
using System.Windows.Browser;


namespace SilverlightAnalytics
{
    /// <summary>
    /// An event tracker for Google Analytics.
    /// </summary>
    /// <typeparam name="TCategory">An <see cref="System.Enum"/> that specifies the <see cref="Category"/> name</typeparam>
    /// <typeparam name="TAction">An <see cref="System.Enum"/> that specifies the types of actions for this <see cref="EventTracker{TCategory,TAction}"/></typeparam>
    /// <remarks><para>This class implements the <a href="http://code.google.com/apis/analytics/docs/eventTrackerOverview.html">Google Event Tracker API</a></para>
    /// <para>For additional information about Event Tracking, check out <a href="http://www.google.com/support/analytics/bin/answer.py?answer=79393&amp;ctx=sibling" title="What is Event Tracking?">this document</a> from Google.  
    /// For more info on the data model used by Event Tracking, <a href="http://www.epikone.com/blog/2007/10/16/event-tracking-pt-1-overview-data-model/" title="Event Tracking Pt. 1: Overview &amp; Data Model">see this article</a></para>
    /// </remarks>
    /// <example>
    /// <para>This simple example shows how to track a pause button on a hypothetical video control.</para>
    /// <code lang="JavaScript" title="Analytics Tracking Code" filter="false">
    /// <![CDATA[
    ///    
    ///         var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    ///         document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    ///         var pageTracker = _gat._getTracker("your-code");  // <---- pageTracker is what we use!!!
    /// ]]>
    /// </code>
    /// <code lang="cs" title="Your Sample App" filter="false">
    /// 
    /// // a bunch of categories
    /// public enum MyCagetories {
    ///     Video,
    ///     Music
    /// }
    /// 
    /// // a bunch of actions
    /// public enum MyActions {
    ///     Play,
    ///     Pause
    /// }
    /// 
    /// // a stupid user control with a pause button.
    /// public partial class TestControl : UserControl
    /// {
    ///     private EventTracker&lt;MyCategories, MyActions&gt; videoTracker;
    ///     public TestControl() {
    ///         // create instance of event tracker for Videos
    ///         videoTracker = new EventTracker&lt;MyCategories, MyActions&gt;(MyCategories.Video);
    /// 
    ///     }
    ///     
    ///     // The Click event for some button
    ///     private void PauseButton_Click(object sender, RoutedEventArgs e)
    ///     {
    ///         // track when the user clicks on this button!
    ///         videoTracker.TrackEvent(MyActions.Pause );
    ///     }
    /// }
    /// </code>
    /// </example>
    public class EventTracker<TCategory, TAction> : PageTracker 
    {
        

        /// <summary>
        /// Our private constructor
        /// </summary>
        private EventTracker() : base()
        {

        }

        /// <summary>
        /// Creates a new instance of <see cref="EventTracker{TCategory,TAction}"/>
        /// </summary>
        /// <param name="category">The Category <see cref="System.Emun"/></param>
        public EventTracker(TCategory category)
            : this()
        {
            this.Category = category;


        }

        /// <summary>
        /// Gets the Category associated with this <see cref="EventTracker{TCategory,TAction}"/>
        /// </summary>
        public TCategory Category { get; protected set; }

        /// <overloads>
        /// <summary>
        /// Constructs and sends the event tracking call to the Google Analytics Tracking Code. Use this to track visitor behavior on your website that is not related to a web page visit, such as interaction with a Flash video movie control or any user event that does not trigger a page request.
        /// </summary>
        /// <example>
        /// <para>This simple example shows how to track a pause button on a hypothetical video control.</para>
        /// <code lang="JavaScript" title="Analytics Tracking Code" filter="false">
        /// <![CDATA[
        ///    
        ///         var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
        ///         document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
        ///         var pageTracker = _gat._getTracker("your-code");  // <---- pageTracker is what we use!!!
        /// ]]>
        /// </code>
        /// <code lang="cs" title="Your Sample App" filter="false">
        /// 
        /// // a bunch of categories
        /// public enum MyCagetories {
        ///     Video,
        ///     Music
        /// }
        /// 
        /// // a bunch of actions
        /// public enum MyActions {
        ///     Play,
        ///     Pause
        /// }
        /// 
        /// // a stupid user control with a pause button.
        /// public partial class TestControl : UserControl
        /// {
        ///     private EventTracker&lt;MyCategories, MyActions&gt; videoTracker;
        ///     public TestControl() {
        ///         // create instance of event tracker for Videos
        ///         videoTracker = new EventTracker&lt;MyCategories, MyActions&gt;(MyCategories.Video);
        /// 
        ///     }
        ///     
        ///     // The Click event for some button
        ///     private void PauseButton_Click(object sender, RoutedEventArgs e)
        ///     {
        ///         // track when the user clicks on this button!
        ///         videoTracker.TrackEvent(MyActions.Pause );
        ///     }
        /// }
        /// </code>
        /// </example>
        /// </overloads>
        /// <summary>
        /// Constructs and sends the event tracking call to the Google Analytics Tracking Code. Use this to track visitor behavior on your website that is not related to a web page visit, such as interaction with a Flash video movie control or any user event that does not trigger a page request.
        /// </summary>
        /// <param name="action">A <typeparamref name="TAction"/> that is uniquely paired with each <typeparamref name="TCategory"/>, and commonly used to define the type of user interaction for the web object.</param>
        public void TrackEvent(TAction action)
        {
            if (Tracker != null)
            {
                Tracker.Invoke("_trackEvent", Category.ToString(), action.ToString());
            }
        }
        /// <summary>
        /// Constructs and sends the event tracking call to the Google Analytics Tracking Code. Use this to track visitor behavior on your website that is not related to a web page visit, such as interaction with a Flash video movie control or any user event that does not trigger a page request.
        /// </summary>
        /// <param name="action">A <typeparamref name="TAction"/> that is uniquely paired with each <typeparamref name="TCategory"/>, and commonly used to define the type of user interaction for the web object.</param>
        /// <param name="label">A <see cref="String"/> to provide additional dimensions to the event data.</param>
        public void TrackEvent(TAction action, String label)
        {
            if (Tracker != null)
            {
                Tracker.Invoke("_trackEvent", Category.ToString(), action.ToString(), label);
            }
        }
        /// <summary>
        /// Constructs and sends the event tracking call to the Google Analytics Tracking Code. Use this to track visitor behavior on your website that is not related to a web page visit, such as interaction with a Flash video movie control or any user event that does not trigger a page request.
        /// </summary>
        /// <param name="action">A <typeparamref name="TAction"/> that is uniquely paired with each <typeparamref name="TCategory"/>, and commonly used to define the type of user interaction for the web object.</param>
        /// <param name="label">A <see cref="String"/> to provide additional dimensions to the event data.</param>
        /// <param name="value">An integer that you can use to provide numerical data about the user event. </param>
        public void TrackEvent(TAction action, String label, int value)
        {
            if (Tracker != null)
            {
                Tracker.Invoke("_trackEvent", Category.ToString(), action.ToString(), label, value);
            }
        }


    }
}
